"""
"""
import unittest as ut

import basicVdsTest as vdst
from confUtils import Validate
import vdsClient as vdsc
from storageConnectionTestUtils import eStorageType, disconnectFromStorage, connectToStorage, validateConnections, CONNECTIONS_OPTION_DEFAULT
from storageDomainTestUtils import eDomainClass, generateDomainTargets, destroyDomainTargets

__commonConfig__ = {
    "VdsUri1" : {"default" : "http://server:54321"},
    "Connections" : {"default" : CONNECTIONS_OPTION_DEFAULT, "validator" : validateConnections},
    "DomainClass1" : {"default" : "data", "validator" : eDomainClass.parse},
    "Domain1" : {"default" : "GUID, GUID", "validator" : Validate.list},
    "StorageType1": {"default" : "nfs", "validator" : eStorageType.parse},
}

class UnicodeArgumentTest(vdst.BasicVDSTest):
    #For convenience. I didn't put it in the base class because I didn't want to commit.
    mycfg = property(lambda self: self.cfg[self.__class__.__name__])
    @classmethod
    def getConfigTemplate(cls):
        return { cls.__name__ : __commonConfig__ }

    def setUp(self):
        #Setup client
        self.client = vdsc.VDSClient()
        self.client.setURI(self.mycfg["VdsUri1"])

        #Make sure env is clean
        resp = self.assertVdscOK(self.client.getConnectedStoragePoolsList())
        self.assertTrue(len(resp['poollist']) == 0, "VDSM is connected to a pool. Dirty env. Bailing out.")

        #Handle storage configuration
        connectToStorage(self.client, self.mycfg["Connections"])
        self.domainTarget1 = generateDomainTargets(self.client, self.mycfg["Connections"], [self.mycfg["Domain1"]])[0]

    def testConvertable(self):
        #Setup test variables
        self.sdUUID = self.client.newUUID()
        domainName = u'I AM UNICODE!'
        self.assertVdscOK(
            self.client.createStorageDomain(self.mycfg["StorageType1"], self.sdUUID,
                domainName, self.domainTarget1, self.mycfg["DomainClass1"]
                )
        )

        self.assertVdscOK(
            self.client.formatStorageDomain(self.sdUUID)
            )

    def testNonConvertable(self):
        #Setup test variables
        self.sdUUID = self.client.newUUID()
        domainName = u'\xd7\xa9\xd7\x92\xd7\x99\xd7\x90 \xd7\x94\xd7\x9e\xd7\x9c\xd7\x9a'
        code = self.assertVdscFail(
            self.client.createStorageDomain(self.mycfg["StorageType1"], self.sdUUID,
                domainName, self.domainTarget1, self.mycfg["DomainClass1"]
                )
            )['status']['code']

        self.assertEquals(code, 5000)

    def tearDown(self):
        destroyDomainTargets(self.client, [self.domainTarget1])
        disconnectFromStorage(self.client, self.mycfg["Connections"])

def suite():
    tests = {
        UnicodeArgumentTest: ["testConvertable", "testNonConvertable"]
    }

    resSuite = ut.TestSuite()
    for testcase, methods in tests.iteritems():
        resSuite.addTests(map(testcase, methods))

    return resSuite

validateConfig = lambda cfg : vdst.validateSuiteConfig(suite(), cfg)
